﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Markup;

using System.Windows.Xps.Packaging;
using System.Windows.Xps.Serialization;


using System.Xml.XPath;
using System.Xml.Xsl;


namespace BadHabit.Factory.Controls
{
    /// <summary>
    /// Interaction logic for ReportsPanel.xaml
    /// </summary>
    public partial class ReportsPanel : GenericPanel    {
        
        private List<String> reportTypesHourbank = new List<string>();
        private List<String> reportTypesBacklog = new List<string>();
        private List<String> reportTypesTail = new List<string>();


        private ListBox selectedListBox = null;

        /// <summary>
        /// Default Constructor.
        /// </summary>
        public ReportsPanel() {
            InitializeComponent();
        }

        /// <summary>
        /// Gets the selected report type.
        /// </summary>
        private string ReportTemplate {
            get {
                if (selectedListBox.SelectedItem == null)
                    return null;
                else
                    return selectedListBox.SelectedItem.ToString() + ".xslt";
            }
        }

        private string ReportName {
            get {
                if (selectedListBox == null)
                    return null;
                if (selectedListBox.SelectedItem == null)
                    return null;
                else
                    return selectedListBox.SelectedItem.ToString();
            }
        }
        
        

        /// <summary>
        /// Loads the report.
        /// </summary>
        public void LoadReport() {

            flowDocumentViewer.Document = null;
            

            if (selectedListBox.SelectedItem == null)
                return;

            if (string.IsNullOrEmpty(selectedListBox.SelectedItem.ToString()))
                return;

            if (Project == null)
                return;

            string flowDocumentXAML;
            try {
                if(selectedListBox.Equals(reportMonitoringListBox))
                    flowDocumentXAML = Client.Service.GenerateHourBankReportXAML(Project.ProjectUId, ReportTemplate);
                else if(selectedListBox.Equals(reportBacklogListBox))
                    flowDocumentXAML = Client.Service.GenerateBackLogReportXAML(Project.ProjectUId, ReportTemplate);
                else
                    flowDocumentXAML = Client.Service.GenerateTailReportXAML(Project.ProjectUId, ReportTemplate);

            } catch (Exception ex) {
                Windows.Error.ShowError(ex, Properties.Resources.COULD_NOT_READ_REPORT);
                return;
            }

            Client.FactoryMainWindow.IsWaiting = true;

            System.IO.MemoryStream stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(flowDocumentXAML));
            try {
                FlowDocument document = (FlowDocument)XamlReader.Load(stream);
                flowDocumentViewer.Document = document;
            } catch (Exception ex) {
                Windows.Error.ShowError(ex, Properties.Resources.COULD_NOT_READ_REPORT);
            }

            

            
            

            Client.FactoryMainWindow.IsWaiting = false;
            

            
        }


        
            

        private bool reportTypesLoaded = false;
        private void LoadReportsTypes() {

            if (reportTypesLoaded)
                return;

            if (!Client.IsConnected)
                return;

            try {
                reportTypesHourbank.AddRange(Client.Service.RetriveHourBankReportTemplateList());
                reportTypesBacklog.AddRange(Client.Service.RetriveBacklogReportTemplateList());
                reportTypesTail.AddRange(Client.Service.RetriveTailReportTemplateList());                        
            } catch (Exception ex) {
                Windows.Error.ShowError(ex, Properties.Resources.COULD_NOT_READ_REPORTLIST);
                return;
            }

            for (int i = 0; i < reportTypesHourbank.Count; i++ )
                reportTypesHourbank[i] = reportTypesHourbank[i].Replace(".xslt", "");

            for (int i = 0; i < reportTypesTail.Count; i++)
                reportTypesTail[i] = reportTypesTail[i].Replace(".xslt", "");

            for (int i = 0; i < reportTypesBacklog.Count; i++)
                reportTypesBacklog[i] = reportTypesBacklog[i].Replace(".xslt", "");

            reportBacklogListBox.ItemsSource = reportTypesBacklog;
            reportMonitoringListBox.ItemsSource = reportTypesHourbank;
            reportTailListBox.ItemsSource = reportTypesTail;

            reportTypesLoaded = true;

                                    
        }

        private void Print_Click(object sender, RoutedEventArgs e) {
            flowDocumentViewer.Print();
            
        }

        private void ReportPanel_Loaded(object sender, RoutedEventArgs e) {
            
            if(!reportTypesLoaded)
                LoadReportsTypes();

        }

        private bool isChanging = false;
        private void reportTypeListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {

            if (isChanging)
                return;

            isChanging = true;

            selectedListBox = e.OriginalSource as ListBox;
            if (selectedListBox == null)
                return;


            if (!selectedListBox.Equals(reportMonitoringListBox))
                reportMonitoringListBox.SelectedItem = null;

            if (!selectedListBox.Equals(reportBacklogListBox))
                reportBacklogListBox.SelectedItem = null;

            if (!selectedListBox.Equals(reportTailListBox))
                reportTailListBox.SelectedItem = null;

            LoadReport();

            isChanging = false;

        }

        private void ReportPanel_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) {
            if (!IsSelected) {
                reportBacklogListBox.SelectedItem = null;
                reportMonitoringListBox.SelectedItem = null;
                reportTailListBox.SelectedItem = null;
            } else
                LoadReport();
        }

        private void exportButton_Click(object sender, RoutedEventArgs e) {

            if (ReportName == null)
                return;
                        
            System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog();
            saveDialog.FileName = Project.ClientName + " - " + Project.ProjectName + " - " + ReportName + ".xps";
            saveDialog.Filter = "XPS File|*.xps";

            System.Windows.Forms.DialogResult d = saveDialog.ShowDialog();
            if (d == System.Windows.Forms.DialogResult.Cancel)
                return;

            

            Client.FactoryMainWindow.IsWaiting = true;            
            BadHabit.XPSTools.XPSHelper.FlowDocumentToXps(flowDocumentViewer.Document, saveDialog.FileName, null, BadHabit.XPSTools.XPSHelper.XPSWriteMode.Overwrite);            
            Client.FactoryMainWindow.IsWaiting = false;

            LoadReport();
        }

    }
}
